% Show off some random animated dots
clear
clear classes

dm = dotsTheDrawablesManager.theObject;
dm.serverMode = false;
dm.clientMode = false;

duration = 5;
groupName = 'kinetogram demo';
gridSize = 4;

dotsTheDrawablesManager.reset;
dm = dotsTheDrawablesManager.theObject;

% a kinetogram with minimal motion features
clean = dm.newObject('dotsDrawableRDK');
dm.addObjectToGroup(clean, groupName);
clean.y = gridSize;
clean.x = -gridSize;
clean.direction = 135;
clean.coherence = 50;

% a kinetogram with many motion features
messy = dm.newObject('dotsDrawableRDK');
dm.addObjectToGroup(messy, groupName);
% messy.y = gridSize;
% messy.x = gridSize;
% messy.direction = 45;
messy.coherence = 50;
% messy.isFlickering = false;
% messy.isWrapping = false;
% messy.isLimitedLifetime = false;
% silly.interleaving = 1;
% messy.color = [255 64 0];

% a kinetogram with a *distribution* of motion directions
fancy = dm.newObject('dotsDrawableRDK');
dm.addObjectToGroup(fancy, groupName);
fancy.y = -gridSize;
fancy.x = -gridSize;
fancy.dotSize = 1;
fancy.density = 30;
fancy.direction = 0:359;
fancy.directionWeights = normpdf(fancy.direction, 225, 30);
fancy.coherence = 100;
fancy.color = [255 255 0];

% a kinetogram with dots wandering, all together
silly = dm.newObject('dotsDrawableRDK');
dm.addObjectToGroup(silly, groupName);
silly.y = -gridSize;
silly.x = gridSize;
silly.dotSize = 5;
silly.direction = 315;
silly.coherence = 100;
silly.drunkenWalk = 180;
silly.isMovingAsHerd = true;
silly.interleaving = 1;
silly.color = [0 255 255];

dm.activeGroup = groupName;

%% draw with a loop
loop = topsFunctionLoop;
loop.addFunctionToGroupWithRank({@step, dm}, groupName, 1);
try
    dm.openScreenWindow;
    dm.startAnimating;
    loop.runForGroup(groupName, duration);
    dm.stopAnimating;
    dm.closeScreenWindow;
catch err
    dm.closeScreenWindow;
    rethrow(err)
end